/*
 * Copyright© 2013-2018 YZTC 
 * Author zhenghl 
 * 本公司保留所有下述内容的权利; 
 * 本内容为保密信息，仅限本公司内部使用; 
 * 非经本公司书面许可，任何人不得外泄或用于其他目的; 
*/
package com.wxmp.wxcms.ctrl;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wxmp.core.common.BaseCtrl;
import com.wxmp.core.exception.WxErrorException;
import com.wxmp.core.util.AjaxResult;
import com.wxmp.core.util.UploadUtil;
import com.wxmp.wxapi.process.MpAccount;
import com.wxmp.wxapi.process.WxApiClient;
import com.wxmp.wxapi.process.WxMemoryCacheClient;
import com.wxmp.wxcms.domain.WxQrcode;
import com.wxmp.wxcms.service.WxQrcodeService;
/**
 *
 * @author hermit
 * @version 2.0
 * @date 2018-04-17 10:54:58
 */
@Controller
@RequestMapping("/wxqrcode")
public class WxQrcodeCtrl extends BaseCtrl {
	@Autowired
	private WxQrcodeService entityService; 

    /**
     * 根据Id查询用户标签
     * @param id
     * @return
     */
    @RequestMapping(value = "/getById")
    @ResponseBody
    public AjaxResult getById(Long id){
        WxQrcode  WxQrcode = entityService.getById(id);
        return AjaxResult.success(WxQrcode);
    }
    /**
     * 分页查询
     * @param searchEntity
     * @return
     */
    @RequestMapping(value = "/listForPage")
    @ResponseBody
    public AjaxResult listForPage(WxQrcode searchEntity) {
        List<WxQrcode> list = entityService.listForPage(searchEntity);
        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success();
        }
        return getResult(searchEntity,list);
    }
    /**
     * 生成二维码
     * @param entity
     * @return
     * @throws WxErrorException 
     */
    @RequestMapping(value = "/reset")
    @ResponseBody
    public AjaxResult reset(WxQrcode entity) throws WxErrorException {
        MpAccount mpAccount = WxMemoryCacheClient.getMpAccount();//获取缓存中的唯一账号
        if(entity.getId() != null){
            byte[]  qrcode = WxApiClient.createQRCodeLimit(entity.getId().intValue(),mpAccount);
            String url = UploadUtil.byteToImg(request.getServletContext().getRealPath("/"), qrcode);
            entity.setUrl(url);
            entityService.update(entity);
            return AjaxResult.success(url);
        }else {
            return AjaxResult.failure("二维码批量删除失败");
        }
    }
    /**
     * 修改/添加
     * @param entity
     * @return
     * @throws WxErrorException 
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public AjaxResult update(WxQrcode entity) throws WxErrorException{
        if (entity.getId() != null) {
            entityService.update(entity);
            return AjaxResult.updateSuccess();
        } else {
            entityService.add(entity);
            return AjaxResult.updateSuccess();
        }
    } 
    /**
     * 删除
     * @param entity
     * @return
     * @throws WxErrorException 
     */
    @RequestMapping(value = "/deleteById")
    @ResponseBody
    public AjaxResult deleteById(WxQrcode entity) {
        entityService.delete(entity);
        return AjaxResult.deleteSuccess();
    }
    /**
     * 批量删除
     * @param String[] ids
     * @return
     * @throws WxErrorException 
     */
    @RequestMapping(value = "/deleteBatchIds")
    @ResponseBody
    public AjaxResult deleteBatchIds(String [] ids)  {
        if(null != ids && ids.length>0) {
            int nums = 0;
            for (String id : ids) {
                WxQrcode entity = new WxQrcode();
                entity.setId(Long.parseLong(id));
                entityService.delete(entity);
                nums++;
            } 
            return AjaxResult.deleteSuccess();
        }else {
            return AjaxResult.failure("二维码批量删除失败");
        }
    } 
}
